package com.pan.games.threadgame;


public class ThreadReorder {

	private static int x=0,y=0;
	private static int a=0,b=0;

	/**
	 *
	 * 按正常执行不会出现result
	 * 但百万次中，会出现一次重排序
	 * x=b
	 * y=a
	 * 都在第一行
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		int i=0;
		for(;;){
			i++;
			x=0;y=0;a=0;b=0;
			Thread t1 = new Thread(new Runnable(){
				public void run(){
					a=1;
					x=b;
				}
			});
			Thread t2 = new Thread(new Runnable(){
				public void run(){
					b=1;
					y=a;
				}
			});
			t1.start();
			t2.start();
			t1.join();
			t2.join();
			String result = ""+i+"("+x+","+y+")";
			if(x==0&&y==0){
				System.out.println(result);
				break;
			}else{
				System.out.println(i); 
			}
		}
	}
	
	

}
